<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //模拟trim, 去除字符首尾空格
      function myTrim(char) {
        //先去除字符串首部空格，再去除尾部空格
        // let result = char.replace(/(^\s*)/, "").replace(/(\s*$)/, "");
        //或 简化首尾空格过滤
        let result = char.replace(/(^\s*)|(\s*$)/g, "");
        console.log("原字符：", char, "长度：", char.length, "\n结果：", result, "，长度：", result.length);
        return result;
      }
      console.log(myTrim("  a bc "));
      console.log(myTrim("  123"));
      console.log(myTrim("45 6   "));
    </script>
  </body>
</html>
